• frmCustomerRegistration.cs
  • project /
1 using System;
2 using
System.Collections.Generic;
3 using
System.ComponentModel;
4 using
System.Data;
5 using
System.Drawing;
6 using
System.Linq;
7 using
System.Text;
8 using
System.Windows.Forms;
9 using
System.Data.SqlClient;
10 namespace
WarehouseManagementSystem
11 {
12     
public partial class frmCustomerRegistration : Form
13     {
14         SqlDataReader rdr =
null;
15         DataTable dtable =
new DataTable();
16         SqlConnection con =
null;
17         SqlCommand cmd =
null;
18         DataTable dt =
new DataTable();
19         ConnectionString cs =
new ConnectionString();
20         
public frmCustomerRegistration()
21         {
22             InitializeComponent();
23         }
24
25         
private void Form1_Load(object sender, EventArgs e)
26         {
27
28         }
29      
30         
private void Register_Click(object sender, EventArgs e)
31         {
32             
if (txtUsername.Text == "")
33             {
34                 MessageBox.Show(
"Please enter username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
35                 txtUsername.Focus();
36                 
return;
37             }
38           
39             
if (txtPassword.Text == "")
40             {
41                 MessageBox.Show(
"Please enter password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
42                 txtPassword.Focus();
43                 
return;
44             }
45             
if (txtName.Text == "")
46             {
47                 MessageBox.Show(
"Please enter name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
48                 txtName.Focus();
49                 
return;
50             }
51             
if (txtContact_no.Text == "")
52             {
53                 MessageBox.Show(
"Please enter contact no.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
54                 txtContact_no.Focus();
55                 
return;
56             }
57             
if (txtEmail_Address.Text == "")
58             {
59                 MessageBox.Show(
"Please enter email", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
60                 txtEmail_Address.Focus();
61                 
return;
62             }
63             
try
64             {
65                 con =
new SqlConnection(cs.DBConn);
66                 con.Open();
67                 
string ct = "select username from registration where username=@find";
68
69                 cmd =
new SqlCommand(ct);
70                 cmd.Connection = con;
71                 cmd.Parameters.Add(
new SqlParameter("@find", System.Data.SqlDbType.NChar, 30, "username"));
72                 cmd.Parameters[
"@find"].Value = txtUsername.Text;
73                 rdr = cmd.ExecuteReader();
74
75                 
if (rdr.Read())
76                 {
77                     MessageBox.Show(
"Username Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
78                     txtUsername.Text =
"";
79                     txtUsername.Focus();
80
81
82                     
if ((rdr != null))
83                     {
84                         rdr.Close();
85                     }
86                     
return;
87                 }
88                 con =
new SqlConnection(cs.DBConn);
89                 con.Open();
90                 
string ct1 = "select Email from registration where Email='" + txtEmail_Address.Text + "'";
91
92                 cmd =
new SqlCommand(ct1);
93                 cmd.Connection = con;
94                 rdr = cmd.ExecuteReader();
95
96                 
if (rdr.Read())
97                 {
98                     MessageBox.Show(
"Email ID Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
99                     txtEmail_Address.Text =
"";
100                     txtEmail_Address.Focus();
101
102
103                     
if ((rdr != null))
104                     {
105                         rdr.Close();
106                     }
107                     
return;
108                 }
109                 con =
new SqlConnection(cs.DBConn);
110                 con.Open();
111
112                 
string cb = "insert into Registration(Username,UserType,Password,ContactNo,Email,Name,JoiningDate) VALUES ('" + txtUsername.Text + "','Customer','" + txtPassword.Text + "','" + txtContact_no.Text + "','" + txtEmail_Address.Text + "','" + txtName.Text + "','" + System.DateTime.Now + "')";
113
114                 cmd =
new SqlCommand(cb);
115                 cmd.Connection = con;
116                 cmd.ExecuteReader();
117                 con.Close();
118                 MessageBox.Show(
"Successfully Registered", "User", MessageBoxButtons.OK, MessageBoxIcon.Information);
119     
120             }
121             
catch (Exception ex)
122             {
123                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
124             }
125         }
126
127        
128         
private void CheckAvailability_Click(object sender, EventArgs e)
129         {
130             
try
131             {
132                 
if (txtUsername.Text == "")
133                 {
134                     MessageBox.Show(
"Please enter username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
135                     txtUsername.Focus();
136                     
return;
137                 }
138                 con =
new SqlConnection(cs.DBConn);
139                 con.Open();
140                 
string ct = "select username from registration where username='" + txtUsername.Text + "'";
141
142                 cmd =
new SqlCommand(ct);
143                 cmd.Connection = con;
144                 rdr = cmd.ExecuteReader();
145
146                 
if (rdr.Read())
147                 {
148                     MessageBox.Show(
"Username not available", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
149                     
return;
150                 }
151                 
if (!rdr.Read())
152                 {
153                     MessageBox.Show(
"Username available", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
154                     txtUsername.Focus();
155
156                 }
157                 
if ((rdr != null))
158                 {
159                     rdr.Close();
160                 }
161             }
162             
catch (Exception ex)
163             {
164                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
165             }
166         }
167
168         
private void Email_Address_Validating(object sender, CancelEventArgs e)
169         {
170             System.Text.RegularExpressions.Regex rEMail =
new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
171             
if (txtEmail_Address.Text.Length > 0)
172             {
173                 
if (!rEMail.IsMatch(txtEmail_Address.Text))
174                 {
175                     MessageBox.Show(
"invalid email address", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
176                     txtEmail_Address.SelectAll();
177                     e.Cancel =
true;
178                 }
179             }
180         }
181
182         
private void Name_Of_User_KeyPress(object sender, KeyPressEventArgs e)
183         {
184             e.Handled = !(
char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space);
185         }
186
187         
private void Username_Validating(object sender, CancelEventArgs e)
188         {
189             System.Text.RegularExpressions.Regex rEMail =
new System.Text.RegularExpressions.Regex("^[a-zA-Z0-9_]");
190             
if (txtUsername.Text.Length > 0)
191             {
192                 
if (!rEMail.IsMatch(txtUsername.Text))
193                 {
194                     MessageBox.Show(
"only letters,numbers and underscore is allowed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
195                     txtUsername.SelectAll();
196                     e.Cancel =
true;
197                 }
198             }
199         }
200
201      
202         
private void txtContact_no_KeyPress(object sender, KeyPressEventArgs e)
203         {
204             
if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
205             {
206                 e.Handled =
false;
207             }
208             
else
209             {
210                 e.Handled =
true;
211             }
212         }
213
214         
private void frmCustomerRegistration_FormClosing(object sender, FormClosingEventArgs e)
215         {
216             
this.Hide();
217             frmLogin frm =
new frmLogin();
218             frm.txtUserName.Text =
"";
219             frm.txtPassword.Text =
"";
220             frm.ProgressBar1.Visible =
false;
221             frm.txtUserName.Focus();
222             frm.Show();
223         }
224     }
225 }


Gõ tìm kiếm nhanh...